<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>预收记录列表</title>
    <script type="text/javascript">
      var newGrantWin;
      var editGrantWin;
      var grantId;
      var grantAuditWin;
      var editGrantWin;
      Ext.onReady(function(){
        var customer = Ext.create('Ext.form.ComboBox', {
          fieldLabel: '客户',
          store: customerStore,
          valueField: 'id',
          displayField: 'name',
          name: 'customer_id',
          loadingText: '搜索中...',
          pageSize: 10,
          minChars: 0,
          anchor: '100%',
          listConfig: {
            getInnerTpl: function() {
              return '<div data-qtip="{number}">{name}</div>';
            }
          }
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '收款日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '95%',
          vtype: 'daterange',
          endDateField: 'end_date',
          anchor: '100%'
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至    ',labelWidth:30,
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '95%',
          vtype: 'daterange',
          startDateField: 'begin_date',
          anchor: '100%'
        });

        var searchButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          width:70,
          handler: function(){
            grantDS.removeAll();
            grantDS.getProxy().extraParams.search_type = 'warehouse';
            grantDS.getProxy().extraParams.customer_id = customer.getValue();
            grantDS.getProxy().extraParams.begin_date = beginDate.getRawValue();
            grantDS.getProxy().extraParams.end_date = endDate.getRawValue();
            grantDS.getProxy().extraParams.start = 0;
            grantDS.load();
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 60,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: screenWidth-190},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .25,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [customer]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [beginDate]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [endDate]
                    },{
                      columnWidth: .1,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [searchButton]
                    }, {
                      columnWidth: .1,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });

        var grantDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'Grant',
          proxy : {
            type : 'ajax',
            url: '/grant/getAllGrants',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          autoLoad : true
        });
        

        //生成要订单的产品的Grid
        var grantGrid = Ext.create('Ext.grid.Panel', {
          title:'赠款记录',
          store: grantDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '预收款单号',
              dataIndex: 'receivable_order_number',
              width: 150
            },{
              header: '预收款审批状态',
              dataIndex: 'receivable_auditing_status',
              width: 90,
              renderer: auditingStatusRender
            },{
              header: '客户名称',
              dataIndex: 'customer_name',
              width: 170
            },{
              header: '赠款单号',
              dataIndex: 'order_number',
              width: 150
            },{
              header: '赠款金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 75
            },{
              header: '销售策略',
              dataIndex: 'sales_strategy_name',
              align: 'right',
              width: 75
            },{
              header: '申请变更配赠金额',
              dataIndex: 'new_amount',
              align: 'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 120
            },{
              header: '生效日期',
              dataIndex: 'begin_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '失效日期',
              dataIndex: 'end_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '描述',
              dataIndex: 'description',
              width: 180
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 70
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 55
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            }],
          selModel : Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: 560,
          frame: true,
          tbar: [{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = grantGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  grantId = records[0].get('id');
                  if (receivableId == null){
                    return;
                  }
                  if((records[0].get('auditing_status').indexOf('申请') < 0 && records[0].get('auditing_status').indexOf('通过') < 0 )|| (records[0].get('auditing_status')=='' || records[0].get('auditing_status') == '驳回新增')){
                    editGrantWin = Ext.create('Ext.window.Window', {
                      layout: 'fit',
                      width: 610,
                      height: 275,
                      closeAction: 'destroy',
                      plain: true,
                      modal: true,
                      constrain:true,
                      //modal: true,
                      title: '编辑预收记录',
                      autoLoad: {
                        url: "/grant/editGrant?id="+records[0].get('id'),
                        scripts: true,
                        callback: function(options, success, response, request)
                        {
                          if (response.responseText === "NoPrivilege"){
                            editGrantWin.close();
                          }
                        }
                      }
                    });
                    editGrantWin.on("destroy",function(){
                      grantDS.load();
                    });
                    editGrantWin.resizable = false;
                    editGrantWin.show();
                  }
                }
              }
            },'-',{
              text: '撤消',
              iconCls: 'cancel',
              handler : function(){
                var records = grantGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{                 
                  grantId = records[0].get('id');
                  if (grantId == null){
                    return;
                  }
                  if(records[0].get('auditing_status') != '通过撤消' && records[0].get('auditing_status').indexOf('申请') < 0){
                    Ext.MessageBox.confirm("提示","你确认要申请撤消此收款记录吗？",function(btn){
                      if(btn=="yes"){
                        Ext.Ajax.request({
                          url : '/grant/applyToCancel?id=' + grantId,
                          success : function (result, request) {
                            grantDS.load();
                          },
                          failure : function (result, request){
                            Ext.MessageBox.show({
                              title: '消息',
                              msg: "通讯失败，请从新操作",
                              buttons: Ext.MessageBox.OK,
                              icon: Ext.MessageBox.WARNING
                            });
                          },
                          method : 'GET'
                        });
                      }else{
                        //alert("no");
                      }
                    });
                  }
                }
              }
            }],
          bbar: Ext.create('Ext.toolbar.Paging', {
            pageSize: pageSize + 10,
            store: grantDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });

        grantGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          grantId = grantDS.getAt(index).get('id');
          var auditingStatus = grantDS.getAt(index).get('auditing_status');
          auditForAdd.setDisabled(true);
          auditForCancel.setDisabled(true);
          if (auditingStatus.indexOf('申请新增') > -1 || auditingStatus.indexOf('申请变更') > -1){
            auditForAdd.setDisabled(false);
          }
          if (auditingStatus.indexOf('申请撤消') > -1) {
            auditForCancel.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var auditForAdd = Ext.create('Ext.menu.Item', {
          text: '【审批配赠款项】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });

        var auditForCancel = Ext.create('Ext.menu.Item', {
          text: '【审批配赠款项  <em style=color:red>(撤消)</em>】',
          //iconCls: 'album',
          handler: rightMenuAuditForCancelFn
        });

        function rightMenuAuditFn(){
          grantAuditWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 355,
            height: 240,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '配赠款审批',
            autoLoad: {
              url: "/grant/auditGrant?id="+grantId,
              scripts: true
            }
          });
          grantAuditWin.on("destroy",function(){
            grantDS.load();
          });
          grantAuditWin.resizable = false;
          grantAuditWin.show();
        }

        function rightMenuAuditForCancelFn(){
          grantAuditForCancelWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '配赠审批<em style=color:red>(撤消)</em>',
            autoLoad: {
              url: "/grant/auditGrantForCancel?id="+grantId,
              scripts: true
            }
          });
          grantAuditForCancelWin.on("destroy",function(){
            grantDS.load();
          });
          grantAuditForCancelWin.show();
        }
        
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [auditForAdd,auditForCancel]
        });
        
        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: 650,
          frame:false,
          
          layout: {type: 'table',columns:1},
          
          items:[queryFormPanel,grantGrid]
        });
        workbenchPanel.render("grants_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="grants_workbench_panel"></div>
  </body>
</html>